package cn.hy.dataSync.statistics.dirty;


import cn.hy.dataSync.config.AbstractTaskDirtyCollector;
import cn.hy.dataSync.config.ExpandType;
import cn.hy.dataSync.element.Record;
import cn.hy.dataSync.statistics.communication.Communication;
import cn.hy.dataSync.statistics.communication.CommunicationTool;
import lombok.extern.slf4j.Slf4j;

import java.util.Objects;


/**
 * 脏任务收集器,
 */
@Slf4j
public class TaskDirtyCollector extends AbstractTaskDirtyCollector {

    private final Communication communication;

    private final ExpandType expandType;

    public TaskDirtyCollector(Communication communication,
                              ExpandType expandType) {
        this.communication = communication;
        this.expandType = expandType;
    }

    public Communication getCommunication() {
        return communication;
    }

    @Override
    public void collectDirtyRecord(Record dirtyRecord,
                                   Throwable t,
                                   String errorMessage) {

        if (Objects.isNull(dirtyRecord)) {
            log.warn("脏数据record=null");
            return;
        }

        if (this.expandType.equals(ExpandType.READER)) {
            this.communication.increaseCounter(CommunicationTool.READ_FAILED_RECORDS, 1);
        } else if (this.expandType.equals(ExpandType.WRITER)) {
            this.communication.increaseCounter(CommunicationTool.WRITE_FAILED_RECORDS, 1);
        } else {
            throw new RuntimeException(String.format("不知道的扩展类型[%s].", this.expandType));
        }
    }
}
